﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Web.Services;
using Catering.View_Operaction;
using System.Collections;

namespace Catering.Catering_Page.Caipin
{
    public partial class Caigou_Manage : UserIsNull
    {
        Catering.View_Operaction_Common voc = new View_Operaction_Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind_Data_to_Controls.Bind_Area_ddl(Dr_Operaction, Base_IsAdmin, Base_Groupid, Base_Companyid);
                Bind_Data_to_Controls.Bind_Class_ddl(Dr_Operaction.SelectedItem, Dr_Class, Base_IsAdmin, Base_Classid);
                this.Dr_DateType.SelectedValue = "ri";
                SearchDate.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                SearchDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_zyq, Base_IsAdmin, Base_Groupid, Base_Companyid, false, true);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_zyq.SelectedItem, Drop_bz, Base_IsAdmin, Base_Classid, false);
            if (Base_IsAdmin)
            {
                this.Drop_zyq.Enabled = true;
                this.Drop_bz.Enabled = true;
            }
            this.LoadRep();
        }

        //绑定Rep控件
        public void LoadRep()
        {
            But_Seach_Purchase(null, null);
            //this.AspNetPager1.RecordCount = new Catering_Rules.Purchase().GetCountBywhere("1=1");
            //DataTable dt_Purchase = new Catering_Rules.Purchase().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, "1=1 order by EditDate desc").Tables[0];
            //this.Rep_Caigou.DataSource = dt_Purchase;
            //this.Rep_Caigou.DataBind();
        }

        //获取采购详细信息
        [WebMethod]
        public static ArrayList GetCaigouById(string strid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.Purchase md_p = new Catering_Rules.Purchase().GetModel(Convert.ToInt32(strid));
            if (md_p != null)
            {
                al.Add("OK");
                al.Add(md_p.FMID);
                al.Add(md_p.PPName);
                al.Add(md_p.Back);
                al.Add(md_p.temp1);
                Catering_Model.ClassInfo md_cl = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(md_p.temp1));
                al.Add(md_cl.AreaID);
                return al;
            }
            al.Add("NO");
            return al;
        }

        //根据作业区ID 获取班组下拉选项
        //[WebMethod]
        //public static ArrayList GetClassOption(string AreaID)
        //{
        //    ArrayList al = new ArrayList();
        //    DataTable dtclass=new Catering_Rules.ClassInfo().GetList(" AreaID="+AreaID).Tables[0];
        //    StringBuilder classoption = new StringBuilder();
        //    foreach (DataRow row in dtclass.Rows)
        //    {
        //        classoption.Append("<option value=\"" + row["id"].ToString() + "\">" + row["CName"].ToString() + "</option>");
        //    }
        //    al.Add(classoption);
        //    return al;
        //}

        public string test(Int32 PageID, string strWhere)
        {
            Catering_Rules.ViewOperaction vop = new Catering_Rules.ViewOperaction();
            DataSet ds = new DataSet();
            if (strWhere.Length > 0)
            {
                ds = vop.GetViewToDataSet("Purchase_user", strWhere);
            }
            else
            {
                ds = vop.GetViewToDataSet("Purchase_user");
            }
            StringBuilder sb = new StringBuilder();
            int Pagecount = 0;

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dr_PageCount.SelectedValue != null)
                {
                    Pagecount = Convert.ToInt32(dr_PageCount.SelectedValue);
                }
                else
                {
                    Pagecount = 1;

                }
                if (PageID < 1)
                {
                    PageID = 1;
                }
                if (dt.Rows.Count > Pagecount && dt.Rows.Count >= PageID * Pagecount)
                {
                    for (int i = (PageID - 1) * Pagecount; i < PageID * Pagecount; i++)
                    {
                        int s = i + 1;
                        if (dt.Rows[i] != null)
                        {
                            if (i % 2 != 0)
                            {
                                sb.Append("  <tr>");
                                sb.Append(" <th class=\"left_td\">" + dt.Rows[i]["id"].ToString() + "</th>");
                                sb.Append(" <td>" + dt.Rows[i]["PPName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["Date"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["EditDate"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["bName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["uName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["pcback"].ToString() + "</td>");
                                sb.Append(" <td><a href=\"caigou_Edit.aspx?CGID=" + dt.Rows[i]["id"].ToString() + "\"><img src=\"../../Images/file.gif\" /></a></td>");
                                if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "cgjh.aspx", "操作"))
                                {
                                    sb.Append(" <td><a href=\"#\"><img src=\"../../Images/trash.gif\" onclick=\"Del_Purchase_Info(" + dt.Rows[i]["id"].ToString() + ")\"/></a></td>");
                                }
                                else
                                {
                                    sb.Append("<td>&nbsp;</td>");
                                }
                                sb.Append("</tr>");

                            }
                            else
                            {
                                sb.Append("  <tr class=\"td_bgcolor\">");
                                sb.Append(" <th class=\"left_td\">" + dt.Rows[i]["id"].ToString() + "</th>");
                                sb.Append(" <td>" + dt.Rows[i]["PPName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["Date"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["EditDate"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["bName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["uName"].ToString() + "</td>");
                                sb.Append(" <td>" + dt.Rows[i]["pcback"].ToString() + "</td>");
                                sb.Append(" <td><a href=\"caigou_Edit.aspx?CGID=" + dt.Rows[i]["id"].ToString() + "\"><img src=\"../../Images/file.gif\" /></a></td>");
                                if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "cgjh.aspx", "操作"))
                                {
                                    sb.Append(" <td><a href=\"#\"><img src=\"../../Images/trash.gif\" onclick=\"Del_Purchase_Info(" + dt.Rows[i]["id"].ToString() + ")\"/></a></td>");
                                }
                                else
                                {
                                    sb.Append("<td>&nbsp;</td>");
                                }
                                sb.Append("</tr>");
                            }
                        }
                    }
                    ViewState["count"] = dt.Rows.Count;
                }
                else if (dt.Rows.Count > 0)
                {
                    for (int i = (PageID - 1) * Pagecount; i < dt.Rows.Count; i++)
                    {
                        int s = i + 1;
                        if (i % 2 != 0)
                        {
                            sb.Append("  <tr>");
                            sb.Append(" <th class=\"left_td\">" + dt.Rows[i]["id"].ToString() + "</th>");
                            sb.Append(" <td>" + dt.Rows[i]["PPName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["Date"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["EditDate"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["bName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["uName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["pcback"].ToString() + "</td>");
                            sb.Append(" <td><a href=\"caigou_Edit.aspx?CGID=" + dt.Rows[i]["id"].ToString() + "\"><img src=\"../../Images/file.gif\" /></a></td>");
                            if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "cgjh.aspx", "操作"))
                            {
                                sb.Append(" <td><a href=\"#\"><img src=\"../../Images/trash.gif\" onclick=\"Del_Purchase_Info(" + dt.Rows[i]["id"].ToString() + ")\"/></a></td>");
                            }
                            else
                            {
                                sb.Append("<td>&nbsp;</td>");
                            }
                            sb.Append("</tr>");

                        }
                        else
                        {
                            sb.Append("  <tr class=\"td_bgcolor\">");
                            sb.Append(" <th class=\"left_td\">" + dt.Rows[i]["id"].ToString() + "</th>");
                            sb.Append(" <td>" + dt.Rows[i]["PPName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["Date"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["EditDate"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["bName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["uName"].ToString() + "</td>");
                            sb.Append(" <td>" + dt.Rows[i]["pcback"].ToString() + "</td>");
                            if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "cgjh.aspx", "操作"))
                            {
                                sb.Append(" <td><a href=\"caigou_Edit.aspx?CGID=" + dt.Rows[i]["id"].ToString() + "\"><img src=\"../../Images/file.gif\" /></a></td>");
                                sb.Append(" <td><a href=\"#\"><img src=\"../../Images/trash.gif\" onclick=\"Del_Purchase_Info(" + dt.Rows[i]["id"].ToString() + ")\"/></a></td>");
                            }
                            else
                            {
                                sb.Append("<td>&nbsp;</td>");
                            }
                            sb.Append("</tr>");
                        }
                    }
                    ViewState["count"] = dt.Rows.Count;
                }
            }


            return sb.ToString();
        }

        protected void Dr_Operaction_SelectedIndexChanged(object sender, EventArgs e)
        {
            //voc.DropDownListDataBind_Class(Dr_Class, Dr_Operaction.SelectedValue);
            if (Dr_Operaction.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(Dr_Operaction.SelectedItem, Dr_Class);
            }
        }

        protected void dr_PageCount_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void But_Seach_Purchase(object sender, ImageClickEventArgs e)
        {
            DateTime tempdate = DateTime.MinValue;
            string datetime = this.SearchDate.Text.Trim();
            if (!string.IsNullOrEmpty(datetime))
            {
                DateTime.TryParse(datetime, out tempdate);
            }

            string groupid = this.Dr_Operaction.SelectedValue;
            string strClassID = this.Dr_Class.SelectedValue;

            string strWhere = " 1=1 ";
            if (groupid != "-10")
            {
                if (strClassID != "" && strClassID != "strClassID")
                {
                    strWhere = " temp1=" + strClassID;
                }
                else
                {
                    string tempwhere = "";
                    foreach (ListItem item in this.Dr_Class.Items)
                    {
                        if (item.Value != "-10" && tempwhere == "")
                        {
                            tempwhere = " temp1=" + item.Value;
                        }
                        else if (item.Value != "-10")
                        {
                            tempwhere += " OR temp1=" + item.Value;
                        }
                    }
                    strWhere = " (" + tempwhere + ")";
                }
            }
            else
            {
                string strclasslist = "";
                if (Session["UserID"] != null)
                {
                    string userid = Session["UserID"].ToString();
                    strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(userid);
                }
                if (strclasslist.Length > 0) strWhere = " temp1 in (" + strclasslist + ") ";
            }
            if (tempdate != DateTime.MinValue)
            {
                strWhere += " AND Year(EditDate)=" + tempdate.Year + " AND Month(EditDate)=" + tempdate.Month + " ";
                if (this.Dr_DateType.SelectedValue == "ri" && tempdate.Day > 0) strWhere += " AND Day(EditDate)=" + tempdate.Day + " ";
            }
            this.AspNetPager1.RecordCount = new Catering_Rules.Purchase().GetCountBywhere(strWhere);

            DataTable dt_Purchase = new Catering_Rules.Purchase().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, strWhere + " order by EditDate desc").Tables[0];
            this.Rep_Caigou.DataSource = dt_Purchase;
            this.Rep_Caigou.DataBind();
        }

        protected void but_add_Purchase_Click(object sender, ImageClickEventArgs e)
        {
        }

        protected void buy_select_Page(object sender, EventArgs e)
        {

            Int32 pages = 1;
            Boolean bl = Int32.TryParse(hf_page.Value, out pages);
            if (pages > 0)
            {
                ViewState["Purchase"] = test(pages, "");
            }
            else
            {
                ViewState["Purchase"] = test(1, "");
            }


        }

        //添加采购计划
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Catering_Model.Purchase md = new Catering_Model.Purchase();
            md.id = new Catering_Rules.Purchase().GetMaxId();
            md.PPName = this.txt_PPName.Text;
            md.Date = DateTime.Now;
            md.EditDate = DateTime.Now;
            md.BRid = Convert.ToInt32(Session["UserID"].ToString());
            md.URid = Convert.ToInt32(Session["UserID"].ToString());
            md.Back = this.txt_Back.Text;
            md.temp1 = this.HidZyq.Value;//this.Drop_bz.SelectedValue;
            string RNo = "";
            string Vs = "";
            string Controlled = "";
            GetOtherInfoByClassID(md.temp1, out RNo, out Vs, out Controlled);
            md.RNo = RNo;
            md.Vs = Vs;
            md.Controlled = Controlled;
            new Catering_Rules.Purchase().Add(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');window.location='Caigou_Manage.aspx'</script>");

        }

        public void GetOtherInfoByClassID(string StrClassID, out string RNo, out string Vs, out string Controlled)
        {
            RNo = "";
            Vs = "";
            Controlled = "";

            DataTable dt = new Catering_Rules.Company().GetView_CompanyClass(StrClassID).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                RNo = dt.Rows[0]["BianHao"].ToString();
                Vs = dt.Rows[0]["Vs"].ToString();
                Controlled = dt.Rows[0]["Controlled"].ToString();
            }
            dt.Dispose();
        }

        //修改采购计划
        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            Catering_Model.Purchase oldmd = new Catering_Rules.Purchase().GetModel(Convert.ToInt32(this.HidId.Value));
            Catering_Model.Purchase md = new Catering_Model.Purchase();
            md.id = Convert.ToInt32(this.HidId.Value);
            md.PPName = this.txt_PPName.Text;
            md.Date = oldmd.Date;//只是为了避免时间类型报错
            md.EditDate = DateTime.Now;
            md.BRid = oldmd.BRid;
            md.URid = Convert.ToInt32(Session["UserID"].ToString());
            md.Back = this.txt_Back.Text;
            md.temp1 = this.Drop_bz.SelectedValue;
            string RNo = "";
            string Vs = "";
            string Controlled = "";
            GetOtherInfoByClassID(md.temp1, out RNo, out Vs, out Controlled);
            md.RNo = RNo;
            md.Vs = Vs;
            md.Controlled = Controlled;
            new Catering_Rules.Purchase().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');window.location='Caigou_Manage.aspx'</script>");
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep();
        }

        //获取班组Option
        [WebMethod]
        public static string GetBZOption(string strid)
        {
            DataTable dt_bz = new Catering_Rules.ClassInfo().GetList("AreaID=" + strid).Tables[0];
            return dt_bz.GetOption("id", "CName");
        }

        //通过人员ID 获取人员名称
        public string GetRenName(string strid)
        {
            return new Catering_Rules.T_Personnel().GetModel(Convert.ToInt32(strid)).Name;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Dr_DateType.SelectedValue == "yue")
            {
                SearchDate.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM'})");
                SearchDate.Text = "";
            }
            else
            {
                SearchDate.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                SearchDate.Text = "";
            }
        }

        public string GetClassName(string strid)
        {
            int id = 0;
            Int32.TryParse(strid, out id);
            if (id > 0)
            {
                Catering_Model.ClassInfo classinfo = new Catering_Rules.ClassInfo().GetModel(id);
                if (classinfo != null && !string.IsNullOrEmpty(classinfo.CName)) return classinfo.CName;
            }
            return "";
        }

        public string GetFormateDate(string strdate)
        {
            DateTime date = DateTime.MinValue;
            DateTime.TryParse(strdate, out date);
            if (date > DateTime.MinValue)
            {
                return date.ToString("yyyy-MM-dd");
            }
            return "";
        }
    }
}